package com.heima.xxlJob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class HelloJob {


    @XxlJob("demoJobHandler")
    public void helloJob() {
        log.info("简单任务执行了。。。。");
    }

    @XxlJob("shardingJobHnadler")
    public void shardingJobHandler() {
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        List<Integer> list = getList();
        for (Integer integer : list) {
            if (integer%shardTotal==shardIndex){
                log.info("当前第"+shardIndex+"分片执行了"+integer);
            }
        }
    }

    public List<Integer> getList() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }
        return list;
    }
}